package client;


import java.io.*;
import java.net.ConnectException;
import java.net.Socket;
import java.util.ArrayList;

/**
 * Created by IntelliJ IDEA.
 * User: I
 * Date: 21.11.2010
 * Time: 19:43:26
 * To change this template use File | Settings | File Templates.
 */
public class Client {
    public static ArrayList<String> stringVectors = new ArrayList<String>();
    private static Socket connection;
    private static int port = 8189;
    private static String address = "127.0.0.1";
    private static ObjectOutputStream output_object;
    private static ObjectInputStream input_object;

    public static void main(String[] args) {
        getVectorsFromFile();
        sendData();
        //System.out.println("test read " + stringVectors.get(0) +' '+ stringVectors.get(1));
    }

    public static void getVectorsFromFile() {
        try {
            BufferedReader dataFile = new BufferedReader(new FileReader("data.txt"));

            while (true) {
                String str = dataFile.readLine();
                //System.out.println("test read " + str);
                if (str == null) break;
                stringVectors.add(str);
            }
            dataFile.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            System.out.println("Program can not create file.");
        }
    }

    private static void writeResult(String result) {
        try {
            BufferedWriter resultFile = new BufferedWriter(new FileWriter("result.txt"));
            resultFile.write(result);
            resultFile.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            System.out.println("Program can not create file.");
        }
    }

    private static void sendData() {
        try {
            connection = new Socket(address, port);
            output_object = new ObjectOutputStream(connection.getOutputStream());
            input_object = new ObjectInputStream(connection.getInputStream());

            String st = "";
            for (int i = 0; i < stringVectors.size(); i = i + 2) {
                output_object.writeObject(stringVectors.get(i));
                output_object.writeObject(stringVectors.get(i + 1));

                st += (String) input_object.readObject();
                st += "\n";
                System.out.println("Server text = " + st);
            }
            writeResult(st);
            System.out.println("You are connected.");

        } catch (ConnectException e) {
            System.out.println("Server not found!");
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
